<template>
  <el-card>
    <el-row :gutter='20' class="header">
        <el-col :span='7'>
            <el-input placeholder="请输入医生的名称" clearable v-model="queryForm.query"></el-input>
        </el-col>
        <el-button type="primary" :icon="Search" @click="initGetUsers">{{$t(`table.search`)}}</el-button>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
    <el-table-column :width="item.width" :prop="item.prop" :label="item.label" v-for="(item, index) in options" :key="index">
    <template v-slot="{ row }" v-if="item.prop === 'state'">
        <el-tag
      type="success"
      class="mx-1"
      effect="dark"
      round
      v-if="row.state === '0'"
      >
      已同意
    </el-tag>
    <el-tag
      type="warning"
      class="mx-1"
      effect="dark"
      round
      v-if="row.state === '2'"
      >
      待批阅
    </el-tag>
    <el-tag
      type="danger"
      class="mx-1"
      effect="dark"
      round
      v-if="row.state === '1'"
      >
      已拒绝
    </el-tag>
    </template>
    <template #default="{ row }" v-else-if="item.prop === 'action'">
    <template v-if="row.state === '2'">
      <el-button type="primary" size="small"  @click="handleDialogValue(row)">同意</el-button>
        <el-button type="danger" size="small"  @click="delUser(row)">拒绝</el-button>
    </template>
    <el-button v-if="row.state === '1'||row.state === '0'" type="warning" size="small"  @click="reset(row)">撤销</el-button>
    </template>
    </el-table-column>
  </el-table>
  <el-pagination
      v-model:currentPage="currentPage4"
      v-model:page-size="queryForm.pagesize"
      :page-sizes="[2, 4, 6, 8]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  </template>

<script setup>
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { getApply, editApply } from '@/api/apply.js'
import { options } from './options'
const queryForm = ref({
    query: '',
    pagenum: 1,
    pagesize: 2
})

const total = ref(0)
const tableData = ref([])
const initGetUsers = async() => {
    const res = await getApply()
    console.log(res)
    tableData.value = res.data.datas
    total.value = res.data.total
}
initGetUsers()

const handleSizeChange = (pageSize) => {
    queryForm.value.pagenum = 1
    queryForm.value.pagesize = pageSize
    initGetUsers()
}

const handleCurrentChange = (pageNum) => {
    queryForm.value.pagenum = pageNum
    initGetUsers()
}

const handleDialogValue = async(row) => {
    const label = { aid: row.aid, state: '0' }
    await editApply(label)
    initGetUsers()
}

const delUser = async(row) => {
  const label = { aid: row.aid, state: '1' }
  await editApply(label)
  initGetUsers()
}

const reset = async(row) => {
  const label = { aid: row.aid, state: '2' }
  await editApply(label)
  initGetUsers()
}
</script>

<style lang='scss' scoped>
.header{
    padding-bottom: 16px;
    box-sizing: border-box;
}
</style>
